package com.luo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.luo.constant.MessageConstant;
import com.luo.entity.Result;
import com.luo.pojo.OrderSetting;
import com.luo.service.OrderSettingService;
import com.luo.util.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author Zoro
 * @date 2021年08月28
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;


    @RequestMapping("/upload.do")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result upload(MultipartFile excelFile) {
        try {
            List<String[]> datas = POIUtils.readExcel(excelFile);
            orderSettingService.insertList(datas);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 展示数据
     */
    @RequestMapping("/getOrderSettingByMonth.do")
    @PreAuthorize("hasAuthority('REPORT_VIEW')")
    public Result getOrderSettingByMonth(String date) {
        try {
            List<Map<String, Integer>> data = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 预约设置
     */
    @RequestMapping("/editNumberByDate.do")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {

        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
